home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Imágenes y mapas de bits / ImageScaleIsotropic / ImageScaleIsotropic.cs next >
Encoding:
Text File  |  2002-05-06  |  1.4 KB  |  43 lines

  1. //--------------------------------------------------
  2. // ImageScaleIsotropic.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class ImageScaleIsotropic: PrintableForm
  9. {
  10.      Image image;
  11.  
  12.      public new static void Main()
  13.      {
  14.           Application.Run(new ImageScaleIsotropic());
  15.      }
  16.      public ImageScaleIsotropic()
  17.      {
  18.           Text = "Escalar imagen de forma isotr≤pica";
  19.  
  20.           image = Image.FromFile("..\\..\\..\\Apollo11FullColor.jpg");
  21.      }
  22.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  23.      {
  24.           ScaleImageIsotropically(grfx, image, new Rectangle(0, 0, cx, cy));
  25.      }
  26.      void ScaleImageIsotropically(Graphics grfx, Image image, Rectangle rect)
  27.      {
  28.           SizeF sizef = new SizeF(image.Width / image.HorizontalResolution,
  29.                                   image.Height / image.VerticalResolution);
  30.  
  31.           float fScale = Math.Min(rect.Width  / sizef.Width,
  32.                                   rect.Height / sizef.Height);
  33.  
  34.           sizef.Width  *= fScale;
  35.           sizef.Height *= fScale;
  36.           
  37.           grfx.DrawImage(image, rect.X + (rect.Width  - sizef.Width ) / 2,
  38.                                 rect.Y + (rect.Height - sizef.Height) / 2,
  39.                                 sizef.Width, sizef.Height);
  40.      }
  41. }
  42.  
  43.